package org.example.rental.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.rental.common.Result;
import org.example.rental.domain.RentalGoods;
import org.example.rental.domain.vo.CarGoodsVo;
import org.example.rental.service.RentalGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class RentalGoodsController extends BaseController{


    @Autowired
    RentalGoodsService rentalGoodsService;


    @GetMapping("/home/findHomeSell")
    public Result findHomeSell(){

        List<RentalGoods> goodsList = rentalGoodsService.findHomeSell();
        return Result.succ(goodsList);
    }


    @GetMapping("/toVehicleGoods/{id}")
    public Result toVehicleGoods(@PathVariable(name = "id")Integer vehId){

        RentalGoods rentalGoods = rentalGoodsService.toVehicleGoods(vehId);
        return Result.succ(rentalGoods);
    }


    @PostMapping("/addGoods")
    public Result addGoods(@Validated @RequestBody RentalGoods goods ){
        rentalGoodsService.addGoods(goods);
        return Result.succ(goods);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        RentalGoods goods = rentalGoodsService.getById(id);

        return Result.succ(goods);
    }

    @GetMapping("/list")
    public Result list(String name,String status,String vehNum){
        QueryWrapper<RentalGoods> queryWrapper = new QueryWrapper<RentalGoods>();
        queryWrapper.eq(StrUtil.isNotBlank(status),"status",status)
                .eq(StrUtil.isNotBlank(vehNum),"veh_num",vehNum)
                .like(StrUtil.isNotBlank(name),"heading",name)
                .like(StrUtil.isNotBlank(name),"subtitle",name);

        Page<RentalGoods> page = rentalGoodsService.page(getPage(),queryWrapper);
        System.out.println(page.getTotal());

        return Result.succ(page);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody RentalGoods rentalGoods){
        System.out.println(rentalGoods.getHeadImg());
        rentalGoodsService.updateById(rentalGoods);
        return Result.succ(rentalGoods);
    }


    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        //删除
        rentalGoodsService.deleteByIds(Arrays.asList(ids));

        return Result.succ("删除成功");

    }


    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestBody RentalGoods goods){
        rentalGoodsService.updateById(goods);
        return Result.succ(goods);
    }

    //查询所有上架了的车型
    @PostMapping("/home/selectGoodsByStatus")
    public Result selectGoodsByStatus(@RequestBody Long[] ids,String year,String price){

        QueryWrapper<RentalGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.ge(StrUtil.isNotBlank(price),"price",price);
        queryWrapper.ge(StrUtil.isNotBlank(year),"price",year);

        if (ids.length > 0){
            queryWrapper.in("brand_id",ids);
        }
        List<RentalGoods> goods = rentalGoodsService.list(queryWrapper);
        return Result.succ(goods);
    }




    @GetMapping("/home/findCarDetail")
    public Result findCarDetail(Integer id){
        CarGoodsVo vo = rentalGoodsService.findCarDetail(id);
        return Result.succ(vo);
    }








}
